<?php

// Database.php
// Michael Baker
// c. 2008
// 
// defines a general database object

if ( !defined( "IN_MDM" ) )
  exit;

$dbtime = 0;
$dbtime2 = 0;
  
class DatabaseException extends Exception { }

class Database
{
  protected $info;
  private $active;
  private $link;
  
  private $query;
  private $result;
  private $numrows;
  
  public function __construct( $host, $user, $passwd, $db="" )
  {
    // Store the credentials and open the link, etc.
    $this->info = array( $host, $user, $passwd );
    $this->open();
    if ( $db )
      $this->change_db( $db );
  }
  
  public function __destruct()
  {
    // Database object dying -- clean up
    if ( $this->active )
      $this->close();
      
  }
  
  public function get_host()
  {
    return $this->info[ 0 ];
  }
  
  protected function open()
  {
    // Open the database link
    $array = $this->info;
    $host = $array[ 0 ]; $user = $array[ 1 ]; $passwd = $array[ 2 ];
    
    $timeThen = microtime(true);
    $this->link = mysql_connect( $host, $user, $passwd );
    global $dbtime2; $dbtime2 += ( $host !== "127.0.0.1" ? (microtime(true)-$timeThen) : 0 );
    $this->active = true;
  }
  
  public function close()
  {
    // Close the database link
    
    mysql_close( $this->link );
    $this->active = false;
  }
  
  public function change_db( $db )
  {
    // Change databases
    
    if ( !$this->active )
      throw new DatabaseException( "Failed to select database schema: Database link is inactive!" );
    
    mysql_select_db( $db, $this->link );
  } 
  
  public function execute( $query )
  {
    // Run a query
    
    $timeThen = microtime(true);
    if ( !$this->active )
      throw new DatabaseException( "Failed to query database: Link is inactive!" );
    
    $this->query = $query;
    $this->result = mysql_query( $this->query, $this->link );
    
    global $dbtime; $dbtime += ( ($this->info[0] !== "127.0.0.1") ? (microtime(true) - $timeThen) : 0 );
  }
  
  public function numrows()
  {
    return mysql_num_rows( $this->result );
  }
  
  public function affected()
  {
    return mysql_affected_rows( $this->link );
  }
  
  public function fetchone()
  {
    // Fetch one row
    $row = array();
    
    if ( $this->result )
      $row = mysql_fetch_row( $this->result );
    else
      throw new DatabaseException( "Failed to fetch database row: MySQL result is invalid!" );
    
    return $row;
  }
  
  public function fetchall()
  {
    // Fetch all rows
    $rows = array();
    
    for ( $ctr = 0; $ctr < $this->numrows(); $ctr++ )
    {
      $row = $this->fetchone();
      $rows[] = $row;
    }
    
    return $rows;
  }
  
  public function clean()
  {
    // Clean up result data
    
    $this->query = NULL;
    $this->result = NULL;
    $this->numrows = NULL;
  }
  
  public function insert_id()
  {
    // Return insert ID from previous insert
    return mysql_insert_id( $this->link );
  }
  
  public function escape( &$str )
  {
    $str = mysql_real_escape_string( $str, $this->link );
  }
  
}

?>
